package com.sunq.studio.utils;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Created by sunq on 2019/7/7.
 */
public  class HttpHandler {


    volatile String sss="1";



    public static String httpGet(String url) throws Exception {

        CloseableHttpClient httpClient = null;
        try {
            httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet();
            httpGet.setURI(new URI(url));
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(30000)
                    .setSocketTimeout(30000)
                    .build();
            httpGet.setConfig(requestConfig);
            HttpResponse result = (HttpResponse) httpClient.execute(httpGet);
            String str;
            try {
                /**读取服务器返回过来的json字符串数据**/
                str = EntityUtils.toString(((org.apache.http.HttpResponse) result).getEntity());
                EntityUtils.consume(((org.apache.http.HttpResponse) result).getEntity());
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
            // 这里可以解析视频云点播服务端的响应结果
            return str;
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw e;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            throw e;
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                if (httpClient != null){
                    httpClient.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        }
    }
}

